const app = getApp()
const { cakeApi } = require('../../utils/api/index.js')

Page({
  data: {
    address: '正在获取位置...',
    banners: [
      { id: 1, imageUrl: '/assets/images/index/Carousel-0.jpg' },
      { id: 2, imageUrl: '/assets/images/index/Carousel-1.jpeg' },
      { id: 3, imageUrl: '/assets/images/index/Carousel-2.jpg' }
    ],
    recommendProducts: []
  },

  onLoad: function() {
    this.getLocation();
    this.loadRecommendProducts();
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
  },

  // 获取位置信息
  getLocation: function() {
    wx.getLocation({
      type: 'gcj02',
      success: (res) => {
        const { latitude, longitude } = res;
        this.getAddress(latitude, longitude);
      },
      fail: () => {
        wx.showToast({
          title: '获取位置失败',
          icon: 'none'
        });
      }
    });
  },

  // 根据经纬度获取地址
  getAddress: function(latitude, longitude) {
    wx.request({
      url: 'https://apis.map.qq.com/ws/geocoder/v1/',
      data: {
        location: `${latitude},${longitude}`,
        key: 'YOUR_MAP_KEY', // 需要替换为实际的腾讯地图 key
        get_poi: 0
      },
      success: (res) => {
        if (res.data.status === 0) {
          this.setData({
            address: res.data.result.address
          });
        }
      }
    });
  },

  // 加载轮播图数据
  loadBanners: function() {
    // 模拟数据，实际应该从服务器获取
    this.setData({
      banners: [
        { id: 1, imageUrl: '/assets/banners/banner1.jpg' },
        { id: 2, imageUrl: '/assets/banners/banner2.jpg' },
        { id: 3, imageUrl: '/assets/banners/banner3.jpg' }
      ]
    });
  },

  // 加载推荐商品
  async loadRecommendProducts() {
    try {
      const result = await cakeApi.queryProductList({
        categoryIdList: ['1931604705750945792']
      })
      
      if (result.code === '0000') {
        const productList = result.data.map(item => ({
          id: item.productId,
          name: item.productName,
          price: item.price,
          image: item.images[0],
          tagline: item.tagline  //宣传标语
        }))
        
        this.setData({
          recommendProducts: productList
        })
      }
    } catch (error) {
      console.error('加载推荐商品失败：', error)
      wx.showToast({
        title: '加载失败',
        icon: 'none'
      })
    }
  },

  // 导航到菜单页面
  navigateToMenu: function(e) {
    console.log('点击了预订按钮');
    wx.switchTab({
      url: '/pages/menu/index',
      success: function() {
        console.log('跳转成功');
      },
      fail: function(error) {
        console.error('跳转失败', error);
        wx.showToast({
          title: '跳转失败',
          icon: 'none'
        });
      }
    });
  },

  // 导航到订单页面
  navigateToOrder: function(e) {
    console.log('点击了自提/外卖按钮');
    wx.switchTab({
      url: '/pages/order/index',
      success: function() {
        console.log('跳转成功');
      },
      fail: function(error) {
        console.error('跳转失败', error);
        wx.showToast({
          title: '跳转失败',
          icon: 'none'
        });
      }
    });
  },
  //导航到点单页面，定位到具体的商品上面
  goToMenu: function(e) {
    const id = e.currentTarget.dataset.id;
    wx.setStorageSync('tempProductId', id);
    wx.switchTab({
      url: '/pages/menu/index',
      success: function() {
        console.log('跳转成功');
      },
      fail: function(error) {
        console.error('跳转失败', error);
        wx.showToast({
          title: '跳转失败',
          icon: 'none'
        });
      }
    });
  }
  
})